﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Supplier
{
    /// <summary>
    /// 供应商删除
    /// </summary>
    public class SupplierDeleteHandler : IRequestHandler<SupplierDeleteCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IBaseRepository<Suppliers> supplierRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SupplierDeleteHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierRepository"></param>
        /// <param name="mapper"></param>
        public SupplierDeleteHandler(IBaseRepository<Suppliers> supplierRepository, ILogger<SupplierDeleteHandler> logger)
        {
            this.supplierRepository = supplierRepository;
            this.logger = logger;
        }
        public async Task<Result> Handle(SupplierDeleteCommand request, CancellationToken cancellationToken)
        {
            var result =new Result() { Code=(int)CommonErrorCodes.Delete};

            try
            {
                string sql = $"update Suppliers set IsDelete=1 where SuppliersId={request.SuppliersId}";

                var list = supplierRepository.Update(sql, result);

                if (list > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Msg = "删除成功";
                    result.Data = null;
                    logger.LogInformation("删除成功");
                }
            }
            catch (Exception ex)
            {
                logger.LogError("删除失败" + ex.Message);
                throw;
            }

            return result;
        }
    }
}
